<?php

namespace app\api\controller;

use app\api\controller\Tpmecms;
use think\Db;
use think\Config;

use Yurun\Util\YurunHttp;
use Yurun\Util\HttpRequest;
use think\Cache;
/**
 * 定时
 */
class Crontab extends Tpmecms
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 每分钟执行
     */
    public function min()
    {
        // $chat = new \app\api\controller\Chat;

        // $this->update_unread();//更新未读消息数量

        $this->push_seek();//推送话题


    }

    /**
     * 每小时
     *
     * @return void
     */
    public function hour(){

    }

    /**
     * 每天
     *
     * @return void
     */
    public function every_day()
    {
        
    }



    public function push_seek()
    {
        $subject_time = Config::get('site.subject_time');
        $date = date('Y-m-d ').$subject_time;
        $subject_date = date('Y-m-d');
        // Cache::set($subject_date,false);
        $today = Cache::get($subject_date);
        // halt($today);
        if($today)
            return false;

        if(strtotime($date)<time() ){
            //执行推送话题消息
            // halt('zhix');
            $this->run_push_seek();

            Cache::set($subject_date,time());
        }
        

   
       
    }


    public function run_push_seek()
    {
        //获取话题消息
        $subject = Db::name('subject')->where('date',date('Y-m-d'))->find();
        if(!$subject){
            return false;
        }
        
        $url = 'http://'.Config::get('site.chat_domain').'/sendsubject?content='.$subject['name'];
        $client = new \GuzzleHttp\Client();
        $content = $client->request('GET', $url)->getBody()->getContents();

        echo '成功推送话题:'.$content;
    }




     /**
     * 更新未读消息
     *
     * @return void
     */
    public function update_unread()
    {
        $u = 0;
        $j = 0;
        $list = Db::name('chat')->where('time','<',time()-10)->where('msg_status','1')->select();
        if($list){

            foreach ($list as $key => $val) {
                // halt($val);
                unset($data);
                $data['msg_status'] = '1';
                $data['status'] = '7';//类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
                $data['uid'] = $val['re_uid'];
                $data['user_id'] = $val['send_uid'];
               
                // $data['status'] = '7';
                // halt($data);
                $res = Db::name('msg')
                        ->where($data)
          
                        ->order('time desc')
                        ->find();
                        // halt($res);
                
                $user = Db::name('user')->where('id',$val['send_uid'])->find();
                $data['public_id'] = $val['id'];
                try {
                    switch ($val['type']) {
                        case '1':
                            $title = $user['nickname'].'：'.$val['content'];
                            break;
                        case '11':
                            $title = $user['nickname'].'给你发来图片';
                            break;
                        
                        default:
                            # code...
                            break;
                    }
                    if($res){
                        if($res['title']!=$title){
                            Db::name('msg')->where('id',$res['id'])->update(['title'=>$title,'time'=>$val['time']]);
                            $u++;        
                        }
                        
                    }else{
                        $data['time'] = time();
                        $data['title']  = $title;
                        Db::name('msg')->insert($data);
                        $j++;
                    }
                } catch (\Throwable $th) {
                    // halt($val);
                }
                

            }
        }
        echo '+'.$j,'更新:'.$u;
    }
}
